import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";
import type { RoleBindingList } from "./types";

export class K8sRoleBinding {
  roleBindingUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/rbac.authorization.k8s.io/v1/rolebindings`;
  };
  namespaceRoleBindingUrl = (cluster_name, namespace) => {
    return `/api/v1/proxy/${cluster_name}/k8s/apis/rbac.authorization.k8s.io/v1/namespaces/${namespace}/rolebindings`;
  };

  listRoleBindings(
    cluster_name?: string,
    keywords?: string,
    pageNum?: number,
    pageSize?: number
  ): BaseResponse<RoleBindingList> {
    let url = this.roleBindingUrl(cluster_name);
    const params = {};
    if (keywords) {
      params["search"] = true;
      if (keywords) {
        params["keywords"] = keywords;
      }
      if (pageNum && pageSize) {
        params["pageNum"] = pageNum;
        params["pageSize"] = pageSize;
      }
    }
    return http.get(url, params);
  }

  deleteRoleBinding(cluster_name, namespace, name) {
    return http.delete(
      `${this.namespaceRoleBindingUrl(cluster_name, namespace)}/${name}`
    );
  }

  getRoleBinding(cluster_name, namespace, name) {
    return http.get(
      `${this.namespaceRoleBindingUrl(cluster_name, namespace)}/${name}`
    );
  }

  createRoleBinding(cluster_name, namespace, data) {
    return http.post(
      `${this.namespaceRoleBindingUrl(cluster_name, namespace)}`,
      data
    );
  }

  updateRoleBinding(cluster_name, namespace, name, data) {
    return http.put(
      `${this.namespaceRoleBindingUrl(cluster_name, namespace)}/${name}`,
      data
    );
  }
}
